package com.lry.crawlers.fiction.pipeline;

import com.lry.crawlers.fiction.entity.*;
import com.lry.crawlers.fiction.service.FictionChapterService;
import com.lry.crawlers.fiction.service.FictionContentService;
import com.lry.crawlers.fiction.service.FictionService;
import com.lry.crawlers.fiction.service.FictionUpdateService;
import com.lry.crawlers.message.entity.Message;
import com.lry.crawlers.message.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class FictionPipeline implements Pipeline {
    @Autowired
    FictionService fictionService;
    @Autowired
    FictionChapterService fictionChapterService;
    @Autowired
    FictionContentService fictionContentService;
    @Autowired
    FictionUpdateService fictionUpdateService;
    @Autowired
    MessageService messageService;




    public void process(ResultItems resultItems, Task task) {
        //获取数据
        Map<String,Object> map= resultItems.get("data");

        //判断数据是否不为空
        if(map!= null) {
            //判断数据类型
            if (map.get("key").equals("fictionChapter")){
                fiction(map);
                chapter(map);
            }else if(map.get("key").equals("chapter")){
                chapter(map);
            }else if(map.get("key").equals("content")){
                content(map);
            }


        }
    }
    //处理小说搜索
    public void fiction(Map<String,Object> map) {
        Fiction fiction = null;
        String title = (String)  map.get("title");
        String desc = (String)  map.get("desc");
        String author = (String)  map.get("author");
        String fictionType =(String)  map.get("fictionType");
        String cover = (String)  map.get("cover");
        String updateTime = (String)  map.get("updateTime");
        String fictionId = (String) map.get("fictionId");

        Integer id = (Integer) map.get("id");
        fiction = new Fiction();
        fiction.setTitle(title.trim());
        fiction.setAuthor(author.trim());
        fiction.setCreationTime(new Date());
        fiction.setDescs(desc.trim());
        fiction.setFictionId(id.toString()+fictionId);
        fiction.setFictionType(fictionType.trim());
        fiction.setCover(cover);
        fiction.setFictionVariableId(id);
        fiction.setUpdateTime(updateTime);

        try {
            //添加到mysql
            int i= fictionService.setFiction(fiction);
            if(i==0){
                messageService.setMessage(new Message("mysql小说搜索阶段入库失败,fictionId:"+id.toString()+fictionId,new Date()));
            }
        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("小说搜索阶段入库失败,fictionId:"+id.toString()+fictionId+" .Extends："+e,new Date()));
        }
    }


    //处理小说章节
    public void chapter(Map<String,Object> map){


        FictionChapter fictionChapter = null;
        List<String> chapterTitle = (List<String>) map.get("chapterTitle");
        List<String> chapterId = (List<String>) map.get("chapterId");
        String fictionId = (String) map.get("fictionId");
        Integer xsize = (Integer) map.get("chapterNum");
        Integer id = (Integer) map.get("id");
        List<FictionChapter> fictionChapters = new ArrayList<>();
        String updateUrl = (String) map.get("updateUrl");
        updateUrl = "/"+updateUrl.split("/", 4)[3];
        for (int i =0;i<chapterTitle.size();i+=1) {
            fictionChapter = new FictionChapter();
            fictionChapter.setChapterId(id.toString()+chapterId.get(i));
            fictionChapter.setFictionId(id.toString()+fictionId);
            fictionChapter.setTitle(chapterTitle.get(i).trim());
            fictionChapter.setCreationTime(new Date());
            fictionChapter.setFictionVariableId(id);
            fictionChapters.add(fictionChapter);
        }

        try {
            //添加到mysql
            int i  =fictionChapterService.setFictionChapters(fictionChapters);
            if(i==0){
                messageService.setMessage(new Message("mysql小说章节阶段入库失败,fictionId:"+id.toString()+fictionId,new Date()));
            }else{
                int upi = 0;
                if(xsize!=null){
                    fictionService.setupdateTime(new Fiction(id.toString()+fictionId,(String) map.get("updateTime")));
                    upi = fictionUpdateService.setFictionUpdateUp(new FictionUpdate(id.toString()+fictionId,updateUrl,xsize,id,(String) map.get("updateTime")));
                }else {
                    upi = fictionUpdateService.setFictionUpdate(new FictionUpdate(id.toString()+fictionId,updateUrl,fictionChapters.size(),id,(String) map.get("updateTime")));
                }

                if(upi==0){
                    messageService.setMessage(new Message("小说章节更新表入库失败,fictionId:"+id.toString()+fictionId,new Date()));
                }
            }
        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("小说章节阶段入库失败,fictionId:"+id.toString()+fictionId+" .Extends："+e,new Date()));
        }
    }

    //处理小说内容
    public void content(Map<String,Object> map){
        List<String> content = (List<String>) map.get("content");
        String chapterId = (String) map.get("chapterId");
        String fictionId = (String) map.get("fictionId");

        Integer id = (Integer) map.get("id");
        FictionVariable fictionVariable  = (FictionVariable) map.get("fictionVariable");
        FictionContent fictionContent = new FictionContent();
        fictionContent.setChapterId(id.toString()+chapterId);
        fictionContent.setFictionId(id.toString()+fictionId);
        fictionContent.setCreationTime(new Date());
        fictionContent.setFictionVariableId(id);
        if(content.size()==0){
            content.add("章节异常，请联系管理员处理");
        }
        fictionContent.setContent(content.toString());

        try {
            //添加到mysql
            int i = fictionContentService.setFictionContent(fictionContent);
            if(i==0){
                messageService.setMessage(new Message("mysql小说内容阶段入库失败,chapterId:"+id.toString()+chapterId,new Date()));
            }

        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("小说内容阶段入库失败,chapterId:"+id.toString()+chapterId+" .Extends："+e,new Date()));
        }
        }
    }

